package cn.stylefeng.guns.core.util;

import cn.stylefeng.guns.core.common.constant.Constants;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * decimal变量获取
 *
 * @author fengshuonan
 * @date 2019-04-04-17:06
 */
public class DecimalUtil {

    /**
     * 获取object的值
     *
     * @author fengshuonan
     * @Date 2019-04-04 17:07
     */
    public static Long getLong(Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof BigDecimal) {
            return ((BigDecimal) object).longValue();
        }
        if (object instanceof BigInteger) {
            return ((BigInteger) object).longValue();
        }
        if (object instanceof Long) {
            return ((Long) object);
        }
        return null;
    }


    /**
     * 计算总价格
     * @param list
     * @param total
     * @return
     */
    public static BigDecimal total(List<Map<String,Object>> list, BigDecimal total){
        for(int i=0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            if(Constants.FLAG_N.equals(map.get("pay_flag"))){
                //计算总金额，计算未支付的
                BigDecimal bigDecimal = new BigDecimal(map.get("semester_tuition")+"");
                total = total.add(bigDecimal);
            }
        }
        return total;
    }
}
